<template lang="pug">
  div
    .main
      .config-form-wrapper.row
        .config-form.col-md-12.col-lg-9.col-xl-7
          .row
            h2.offset-sm-5.title {{title}}
          br
          SmartForm(
            v-if="hasColumns"
            :formList="formList"
            :labelSize="5"
            :columns="columns"
            :values="formValues"
            :buttonList="buttonList")
</template>

<script>
import { map, clone, omitBy } from 'lodash'
import SmartForm from '@/components/SmartForm'

export default {
  name: 'change-password',
  components: {
    SmartForm
  },
  created () {
    this.getData()
  },
  data () {
    return {
      hasColumns: false,
      title: '系统配置',
      buttonList: [{
        name: 'submit',
        label: '提交',
        type: 'complete',
        validate: true,
        funcProps: {
          config: this
        },
        func: ({funcProps}) => {
          const { config } = funcProps
          const data = map(omitBy(config.formValues, (value, key) => {
            return value === config.orginValues[key]
          }), (value, id) => ({ value, id }))
          this.$patch({
            url: '/dxbase/sys/config',
            data
          }).then(res => {
            this.$message({
              type: 'success',
              message: '修改配置成功'
            })
          })
        }
      }],
      orginValues: {},
      formValues: {},
      formList: [],
      columns: {}
    }
  },
  methods: {
    getData () {
      this.$get({
        url: '/dxbase/sys/config'
      }).then(data => {
        if (data) {
          const columns = data.columns
          this.formList = Object.keys(columns)
          this.orginValues = clone(data.data)
          this.formValues = clone(data.data)
          this.hasColumns = true
          this.formList.map(formItem => {
            const column = columns[formItem]
            const { relation, editable, form } = column
            if (relation !== '') {
              column.options = data.relation[relation]
            }
            // 不允许修改
            if (!editable) {
              columns[formItem].form.type = 'text'
              columns[formItem].minLength = 200
            }
            // 设置前后缀
            if (form.prefix || form.suffix) {
              const type = columns[formItem].form.suffix ? 'suffix' : 'prefix'
              const addonObj = columns[formItem].form[type]
              columns[formItem].form[type] = this.renderFormItemAddon(addonObj)
            }
            if (column.form && column.form.rules) {
              column.form.rules.map((rule) => {
                console.log(rule.type)
                if (rule.type === 'reg') {
                  rule.value = new RegExp(rule.value)
                }
              })
            }
          })
          this.columns = columns
        }
      })
    },
    renderFormItemAddon (obj) {
      return {
        ...obj,
        type: 'success'
      }
    }
  }
}
</script>

<style lang="sass" scoped>
.config-form-wrapper
  margin: 40px
.config-form
  margin: 0 auto
  .title
    padding-left: 10px
</style>
